package com.jeremy.myapplication

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View


/**
 * Auth :jeremy.wang
 * Date :2022/8/17
 * Des :
 **/
class CustomView : View {

    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.BLUE
        style = Paint.Style.FILL
    }
    private var mDstBmp: Bitmap? = null
    private var mSrcBmp: Bitmap? = null

    constructor(context: Context) : super(
        context
    )

    constructor(context: Context, attrs: AttributeSet?) : super(
        context,
        attrs
    )

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        //硬件加速不支持的函数列表中 PorterDuffXfermode是部分不支持的
        //所以 关闭硬件加速 和 使用离屏绘制
        setLayerType(LAYER_TYPE_SOFTWARE, null)
        mDstBmp = makeDst(width, height)
        mSrcBmp = makeSrc(width, height)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)

    }

    private var mMode: PorterDuff.Mode = PorterDuff.Mode.ADD

    //为展示demo状态
    private var mShowDemo = true

    //只看目标图层
    private var mShowDst = false

    //只看源图层
    private var mShowSrc = false

    //看目标图层
    fun showDst() {
        mShowDemo = false
        mShowSrc = false
        mShowDst = true

        invalidate()
    }

    fun showSrc() {
        mShowDst = false
        mShowDemo = false
        mShowSrc = true

        invalidate()
    }

    fun showDemo() {
        mShowSrc = false
        mShowDst = false
        mShowDemo = true
        invalidate()
    }

    fun setMode(mode: PorterDuff.Mode) {
        mMode = mode
        invalidate()
    }

    //谷歌的案例有误导，是否为谷歌的
    private var mTypeIsGoogle = false

    fun setIsGoogle(isGoogle: Boolean) {
        mTypeIsGoogle = isGoogle
        mDstBmp = makeDst(width, height)
        mSrcBmp = makeSrc(width, height)
        invalidate()
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {

        canvas?.let {

            if (mShowDemo) {
                val save = it.saveLayer(0f, 0f, width.toFloat(), height.toFloat(), null)
                drawDst(it)
                mPaint.xfermode = PorterDuffXfermode(mMode)
                drawSrc(it)
                mPaint.xfermode = null
                it.restoreToCount(save)
            } else if (mShowDst) {
                drawDst(it)
            } else if (mShowSrc) {
                drawSrc(it)
            }
        }
    }
    private val mStrokePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.BLACK
        strokeWidth= 5f
        style = Paint.Style.STROKE
        pathEffect = DashPathEffect(floatArrayOf(10f, 5f), 10f)
    }
    //用于区分 目标图层的实际边界
    private val mStrokePaintDST = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.RED
        strokeWidth= 5f
        style = Paint.Style.STROKE
        pathEffect = DashPathEffect(floatArrayOf(10f, 5f), 10f)
    }
    private fun drawDst(canvas: Canvas) {
        if (mTypeIsGoogle) {
            canvas.drawBitmap(mDstBmp!!, 0f, 0f, mPaint)
        } else {
            canvas.drawBitmap(mDstBmp!!, 0f, 0f, mPaint)
        }
    }

    private fun drawSrc(canvas: Canvas) {
        if (mTypeIsGoogle) {
            //谷歌的demo 目标图层也是从左上角开始绘制的
            canvas.drawBitmap(mSrcBmp!!, 0f, 0f, mPaint)
            canvas.drawRect(0f, 0f,(width * 19 / 20).toFloat(),
                ((height * 19 / 20).toFloat()),mStrokePaint)
        } else {
            canvas.drawBitmap(mSrcBmp!!, (width / 2).toFloat(), (height / 2).toFloat(), mPaint)
            canvas.drawRect((width / 2).toFloat(), (height / 2).toFloat(),width.toFloat(),height.toFloat(),mStrokePaint)
        }
    }

    private fun makeDst(w: Int, h: Int): Bitmap {
        //全屏
        val bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
        val c = Canvas(bm)
        val p = Paint(Paint.ANTI_ALIAS_FLAG)
        p.color = -0x33bc //黄色
        if (mTypeIsGoogle) {
            //谷歌的demo 宽高不是全屏
            c.drawOval(RectF(0f, 0f, (w * 3 / 4).toFloat(), (h * 3 / 4).toFloat()), p)
            c.drawRect(0f, 0f, (w * 3 / 4).toFloat(), (h * 3 / 4).toFloat(),mStrokePaintDST)
        } else {
            c.drawOval(RectF(0f, 0f, w.toFloat(), h.toFloat()), p)
            c.drawRect(0f, 0f, w.toFloat(), h.toFloat(),mStrokePaintDST)
        }
        return bm
    }

    private fun makeSrc(w: Int, h: Int): Bitmap {
        //全屏
        val bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
        val c = Canvas(bm)
        val p = Paint(Paint.ANTI_ALIAS_FLAG)
        p.color = -0x995501// 蓝色
        if (mTypeIsGoogle) {
            //右下的矩形
            c.drawRect(
                (w / 3).toFloat(), (h / 3).toFloat(),
                (w * 19 / 20).toFloat(), (h * 19 / 20).toFloat(), p)
        } else {
            c.drawRect(0f, 0f, w.toFloat(), h.toFloat(), p)
        }

        return bm
    }
}